/*
 * Copyright 2012 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.dialog.navigation.model;

import javax.swing.JApplet;
import netscape.javascript.JSObject;
import offset.nodes.client.model.component.AppletEnvironment;
import offset.nodes.client.model.component.AppletInfo;
import offset.nodes.client.model.component.ComponentDialogParser;

/**
 * Creates an applet from the editing page of a node, i.e. the page with URL parameter "edit=true".
 * 
 * @author
 * Walter
 * Lütgenau
 */
public class ComponentDialogEnvironment {
    JApplet base;
    JApplet component;
    String parameters;
    JSObject callingNode;
    JSObject event;

    /**
     * Create a component editor from the uuid only
     * 
     * @param parameters the applet parameters
     */
    public ComponentDialogEnvironment(JApplet base, String parameters, JSObject callingNode, JSObject event) {
        this.base = base;
        this.parameters = parameters;
        this.callingNode = callingNode;
        this.event = event;
    }
    
    /**
     * Init the dialog.
     * 
     * Parse the parameters and create an applet environment from the result.
     * Let the applet environment create the applet.
     */
    public void init() {
        try {
            ComponentDialogParser appletParser = new ComponentDialogParser(parameters);
            AppletInfo info = appletParser.getAppletInfo();
            if (info == null)
                return;

            AppletEnvironment appletEnvironment = new DialogAppletEnvironment(base, info, callingNode, event);
            this.component = appletEnvironment.getApplet();
            component.init();
            component.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }        
    }

    /**
     * Return the created applet, associated to the artificial applet environment.
     * 
     * @return the applet
     */
    public JApplet getComponent() {
        return component;
    }
    
}
